package com.park.dailytool.data.dao.jpa.local;

import com.park.dailytool.data.pojo.model.local.OrderPay;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

/**
 * @author huangding
 * @description
 * @date 2018/12/7 15:25
 */
public interface OrderPayJpaDao extends JpaRepository<OrderPay, Integer> {

    OrderPay findByOrderPayNo(String orderNo);

    List<OrderPay> findByOrderStatus(Integer orderStatus);

    /**
     * 获取传入类型的最后一笔订单
     * @return
     */
    @Query(value = "select * from order_pay where order_type = (?1) and car_account_id=(?2)  and order_status=1   order by id desc limit 1", nativeQuery = true)
    OrderPay findByOrderTypeLastOrder(Integer type, Integer carId);

    /**
     * 查询一段时间内的充值订单
     */
    @Query(value = "select * from order_pay where order_type = ?1 and pay_time>=(?2)  and pay_time<=(?3)", nativeQuery = true)
    List<OrderPay> findByPayDateAll(Integer orderType, LocalDateTime starDate,
        LocalDateTime endDate);


    @Query(value = "select ifnull(sum(ifnull(o.receive_amount,0)),0)  from order_pay o left join car_account c on c.id =  o.car_account_id where c.merchant_code=?1 and  o.order_type = ?2 and o.create_time>=?3  and o.create_time<=?4", nativeQuery = true)
    int sumByOrderTypeDateAll(Integer merCode, Integer orderType, LocalDateTime starDate,
        LocalDateTime endDate);





}
